package com.young.manager.controller;

import com.young.common.interf.IBaseController;
import com.young.common.model.ResultData;
import com.young.manager.model.SysUser;
import com.young.manager.service.SysUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("sysUser")
@RestController
@Slf4j
@RequiredArgsConstructor
public class SysUserController implements IBaseController {

    private final SysUserService sysUserService;

    @GetMapping("")
    ResultData<?> queryUserList(
        @RequestParam(required = false) String account,
        @RequestParam(required = false) String name,
        @RequestParam(required = false) String phone,
        @RequestParam(required = false) Integer status,
        @RequestParam(required = false) Integer pageNum,
        @RequestParam(required = false) Integer pageSize

    ) {
        pageNum = null == pageNum ? DEFAULT_PAG_ENUM : pageNum;
        pageSize = null == pageSize ? DEFAULT_PAGE_SIZE : pageSize;
        return success(sysUserService.queryUserList(account, name, phone, status, pageNum, pageSize));
    }

    @GetMapping("role")
    ResultData<?> queryUserRole(@RequestParam Long userId, @RequestParam(required = false) Boolean simple) {
        simple = null == simple;
        return success(sysUserService.queryUserRoleIds(userId, simple));
    }

    @PostMapping("insert")
    ResultData<?> insertUser(@RequestBody SysUser user) {
        sysUserService.insertSysUser(user);
        return success();
    }

    @PostMapping("update")
    ResultData<?> updateUser(@RequestBody SysUser user) {
        sysUserService.updateSysUser(user);
        return success();
    }

    @PostMapping("resetPassword")
    ResultData<?> resetPassword(@RequestParam Long id, @RequestParam(required = false) String newPassword) {
        sysUserService.resetPassword(id, newPassword);
        return success();
    }

    @PostMapping("delete")
    ResultData<?> deleteUser(@RequestParam Long id) {
        sysUserService.deleteSysUser(id);
        return success();
    }

}
